from typing import Optional

from sqlmodel import Field, SQLModel

from app.models.base import SQLBaseModel, IdType
from app.schemas.base_schema import LoginAccountType, DeviceType


class BaseLog(SQLModel):
    user_id: IdType = Field(index=True, description="用户ID")
    account: str = Field(index=True, description="用户账户")
    account_type:Optional[LoginAccountType] = Field(default=LoginAccountType.username,index=True, description="当前登录使用的账户类型")

    api_path: str = Field(index=True, description="接口地址")

    ip_address: str = Field(index=True, description="IP地址")
    browser: Optional[str] = Field(default=None, description="浏览器")
    os: Optional[str]= Field(default=None, description="操作系统")
    device_id: Optional[str] = Field(default=None, description="设备ID")
    device_type: Optional[DeviceType] = Field(default=None, description="设备类型")
    user_agent: str = Field(description="用户代理信息")

    elapsed_time: int = Field(index=True, description="耗时（毫秒）")
    status_code: int = Field(index=True, description="状态码")
    is_success: bool = Field(index=True, description="是否成功")
    message: Optional[str] = Field(default=None, description="消息")
    result: Optional[str] = Field(default=None, description="返回结果")


class LoginLog(SQLBaseModel, BaseLog, table=True):
    __tablename__ = "login_log"
